﻿Ext.onReady(function() {
    //还可以获得属性
    Ext.fly('load-status').setStyle({

        display: 'none'

    })
    /* 
    Overrides for fixing clearOnLoad for TreeStore 
    treeStore 多次加载
    */
    Ext.override(Ext.data.TreeStore, {
        load: function(options) {
            options = options || {};
            options.params = options.params || {};

            var me = this,
node = options.node || me.tree.getRootNode(),
root;

            // If there is not a node it means the user hasnt defined a rootnode yet. In this case lets just 
            // create one for them. 
            if (!node) {
                node = me.setRootNode({
                    expanded: true
                });
            }

            if (me.clearOnLoad) {
                node.removeAll(false);
            }

            Ext.applyIf(options, {
                node: node
            });
            options.params[me.nodeParam] = node ? node.getId() : 'root';


            if (node) {
                node.set('loading', true);
            }

            return me.callParent([options]);
        }
    });

    // 创建tab选项卡 

    var tabs = Ext.createWidget('tabpanel', {
        id: "MainTab",
        //autoDestroy: true,
        region: 'center',
        contentEl: 'center-div',
        activeTab: 0,
        //width: 600,
        //height: 400,
        split: true,
        border: true,
        lain: true,
        defaults: {
            autoScroll: true,
            bodyPadding: 0
        },
        items: [{
            id:"sys_info",
            title: "系统信息",
            loader: {
                url: '/manager/Sys_Info/Index',
                contentType: 'html',
                autoLoad: true,
                scripts: true,
                scope: this,
                loadMask: true
            },
            closable: true,
            enableTabScroll: true, //挤的时候能够滚动收缩
            autoDestroy: true

}],
            plugins: Ext.create('Ext.ux.TabCloseMenu', {
                extraItemsTail: [
                '-',
                {
                    text: 'Closable',
                    checked: true,
                    hideOnClick: true,
                    handler: function(item) {
                        currentItem.tab.setClosable(item.checked);
                    }
                }
            ],
                listeners: {
                    aftermenu: function() {
                        currentItem = null;
                    },
                    beforemenu: function(menu, item) {
                        var menuitem = menu.child('*[text="Closable"]');
                        currentItem = item;
                        menuitem.setChecked(item.closable);
                    }
                }
            })


        })


        // tree start create('Ext.Viewport'

        var MainTreeStore = Ext.create('Ext.data.TreeStore', {
            fields: ['id', 'text', 'url'],
            proxy: {
                type: 'ajax',
                url: '/Manager/Home/TreeList'
            },
            root: {
                text: '系统菜单',
                id: 'src',
                url: "",
                expanded: true
            },
            folderSort: true,
            sorters: [{
                property: 'text',
                direction: 'ASC'
}]
            });

            var tree = Ext.create('Ext.tree.Panel', {
                title: '功能菜单',
                id: 'MainTree',
                region: 'west',
                store: MainTreeStore,
                width: 150,
		height: document.body.clientHeight-50,
                contentEl: 'west-div',
                split: true,
                border: true,
                collapsible: true,
                minSize: 120,
                maxSize: 360,
                listeners: {
                    'itemclick': function(view, node) {
                        if (node.data.url != "") {
                            //alert("添加" + node.data.id + "    " + node.data.url);
                            var centerPanel = Ext.getCmp("MainTab");
                            // 根据id获取centerPanel组件的直接子组件的引用
                            var tab = centerPanel.getComponent(node.data.id);
                            if (!tab) {
                                // 这个要传到load页去，很关键，以后的布局要靠它
                                //console.log("添加" + node.data.id + "    " + node.data.url);
                                var tabTemp = tabs.add({
                                    id: node.data.id,
                                    title: "&nbsp;" + node.data.text + "&nbsp;",
                                    loader: {
                                        url: '/manager' + node.data.url,
                                        contentType: 'html',
                                        autoLoad: true,
                                        scripts: true,
                                        scope: this,
                                        loadMask: true
                                    },
                                    closable: true,
                                    enableTabScroll: true, //挤的时候能够滚动收缩
                                    autoDestroy: true
                                });
                                tabs.setActiveTab(tabTemp);
                                tabs.doLayout();
                            }
                            else {
                                tabs.setActiveTab(tab);
                                // console.log("显示" + node.data.id + "    " + node.data.url);
                            }

                        }
                    }
                    //                            , 'itemcontextmenu': function(view, record, item, index, e) {
                    //                                e.preventDefault();
                    //                                e.stopEvent();
                    //                                rightClick.showAt(e.getXY());
                    //                            }
                }
            });

            // tree end

            // 居顶工具栏
            var topBar = Ext.create('Ext.toolbar.Toolbar', {
                region: 'north',
                contentEl: 'north-div',
                items: [{
                    text: '<img src="/css/images/icons/shbst.gif" style="height:34px" />',
                    width:100,
                    scale: 'medium'

                }, "", "-", "", {
                    id: "userName",
                    text: "",
                    icon: '/css/images/icons/user.png'
                }, "->", "-", {
                    minWidth: 60,
                    text: "站点首页",
                    href: "/",
                    icon: "/css/images/icons/roomtypeicon.gif",
                    handler: function (btn, e) {

                    }
                }, "-", {
                    xtype: 'splitbutton',
                    text: '皮肤',
                    menu: [
                { text: '皮肤1', icon: "/css/images/skin/c0_0.gif", handler: Ext.Function.pass(setSkin, 0) },
                { text: '皮肤2', icon: "/css/images/skin/c1_0.gif", handler: Ext.Function.pass(setSkin, 1)}]
                }, "-", {

                    minWidth: 80,
                    text: "设置为常用页",
                    icon: "/css/images/icons2/asterisk_yellow.png",
                    handler: function (btn, e) {
                    }
                }, "-", {

                    minWidth: 80,
                    text: "修改密码",
                    icon: "/css/images/icons/key.png",
                    handler: function (btn, e) {
                        EditSys_UserPwd();
                    }
                }, "-", {

                    minWidth: 40,
                    text: "帮助",
                    icon: "/css/images/icons/lightbulb.png",
                    handler: function (btn, e) {
                    }
                }, "-", {
                    minWidth: 40,
                    text: "退出",
                    icon: "/css/images/icons/door_out.png",
                    handler: function (btn, e) {
                        Ext.Msg.confirm("提 示!", "您确定要退出吗?", function (btn) {
                            if (btn == "yes") { location.href = '/manager/user/logout'; }
                        });
                    }
                }]
            });

            Ext.create('Ext.Viewport', {
                layout: {
                    type: 'border',
                    padding: 5
                },
                defaults: {
                    split: true
                },
                items: [topBar,
                            tree,
                            tabs
                          ]
            });

            //加载用户信息
            Ext.Ajax.request({
                url: '/manager/home/GetUserInfo',
                method: "POST",
                success: function(d) {
                    //成功后
                    var action = Ext.JSON.decode(d.responseText);

                    if (action.success == true) {
                        topBar.getChildByElement("userName").setText(action.data.U_LoginName);

                    }
                    else {
                    }

                },
                failure: function(form, action) {
                    Ext.MessageBox.alert("提示!", "加载用户信息失败!");
                }
            })
            Ext.WindowManager.setBase(300);
        });

            





